package com.kobako.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by kobako on 2017/4/9.
 * Just a game
 */
public class MySFFactory {

    private static final Logger logger = LoggerFactory.getLogger(MySFFactory.class);
    private static SqlSessionFactory sqlSessionFactory;
    private static final String CONF_PATH = "com/kobako/mybatis/conf.xml";

    public synchronized static SqlSessionFactory getSessionFactory() {
        if (sqlSessionFactory == null) {
            String confPath = CONF_PATH;
            try {
                InputStream inputStream = Resources.getResourceAsStream(confPath);
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
                logger.error("Mybatis config file not found! The path is : "+confPath);
            }
        }

        return sqlSessionFactory;
    }
}